package com.sixks.app.mvc;

import com.sixks.app.domain.ExpressUser;
import com.sixks.app.mvc.cmd.*;
import com.sixks.app.mvc.cmd.exception.ExpressAPIException;
import com.sixks.app.mvc.cmd.exception.UploadPicResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author colin
 * @date: 19/04/2014
 * @time: 09:07
 */
@Controller
@RequestMapping("/test")
public class TestAPIController {

    private static final Logger logger = LoggerFactory.getLogger(TestAPIController.class);

    /**
     * @return
     */
    @RequestMapping(value = "/uploadImage")
    @ResponseBody
    public List<UploadPicResponse> uploadImage(HttpServletRequest request) {

        ExpressUser user = new ExpressUser();
        user.setAccessToken("b458ed49-dc5e-4276-98ac-fffa93392fda");
        user.setUserName("1095216790707");
        user.setRefressToken("3d598277-68c6-41bc-ad18-c8e4e43b09da");

        List<UploadPicResponse> result = new ArrayList<UploadPicResponse>();

        String[] urls = request.getParameterValues("url");

        APIResponse response;
        try {
            for (String url : urls) {
                UploadPic uploadPic = new UploadPic();
                UploadPicRequest up = new UploadPicRequest(url);
                response = uploadPic.doCall(up, user);
                UploadPicResponse tmp = new UploadPicResponse();
                tmp.setUrl(response.getMessage());
                result.add(tmp);
            }
        } catch (ExpressAPIException e) {
            logger.error("图片上传失败", e);
        }

        return result;
    }

    /**
     * @return
     */
    //@RequestMapping("/cate")
    public JSONResponse category(HttpServletRequest request) {

        ExpressUser user = new ExpressUser();
        user.setAccessToken("afa599eb-4e17-4c52-9871-5bea9b8c7597");
        user.setUserName("1095216790707");
        user.setRefressToken("3d598277-68c6-41bc-ad18-c8e4e43b09da");
        request.getSession().setAttribute("cur_user", user);
        try {
            new AttributeByCategoryId().doCall(new APIRequest(), (ExpressUser) request.getSession().getAttribute("cur_user"));
//            new CategoryById().doCall(new APIRequest(), (ExpressUser) request.getSession().getAttribute(CUR_USER));
            //uploadPic.doCall(new UploadPicRequest("http://pic6.nipic.com/20100324/723269_131504417118_2.jpg"),(ExpressUser) request.getSession().getAttribute(CUR_USER));
        } catch (ExpressAPIException e) {
            logger.error("图片上传失败", e);
        }
        JSONResponse json = new JSONResponse();
        json.setState("ok");
        return json;
    }
}
